<template>
  <div>
    <el-input
      prefix-icon="el-icon-search"
      size="mini"
      placeholder="请输入部门名称"
      v-model="filterText"
    >
    </el-input>
    <el-tree
      :data="deps"
      :props="defaultProps"
      :filter-node-method="filterNode"
      :expand-on-click-node="false"
      :default-expanded-keys="defaultKeeys"
      node-key="id"
      ref="tree"
    >
      <span
        class="custom-tree-node"
        style="display: flex; justify-content: space-between; width: 100%"
        slot-scope="{ node, data }"
      >
        <span>{{ data.name }}</span>
        <span>
          <el-button
            type="primary"
            size="mini"
            class="depBtn"
            @click="() => showAddDepViwe(data)"
          >
            添加部门
          </el-button>
          <el-button
            type="danger"
            size="mini"
            class="depBtn"
            @click="() => deleteDep(data)"
          >
            删除部门
          </el-button>
        </span>
      </span>
    </el-tree>
    <el-dialog title="添加部门" :visible.sync="dialogVisible" width="30%">
      <el-form ref="addDepModel" :model="addDepModel" label-width="80px">
        <el-form-item label="部门名称">
          <el-input v-model="addDepModel.name" size="mini"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="dialogVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="addDep">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  components: {},
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val);
    },
  },
  data() {
    return {
      filterText: "",
      deps: [],
      defaultProps: {
        children: "children",
        label: "name",
      },
      dialogVisible: false,
      addDepModel: {
        name: "",
        depPath: "",
        isParent: false,
        parentId: "",
      },
      defaultKeeys: [],
    };
  },
  methods: {
    showAddDepViwe(data) {
      // Object.assign(this.addDepModel, data);
      // Object.keys(this.addDepModel).forEach(key => {this.addDepModel[key] = data[key] || this,this.addDepModel[key]});
      this.addDepModel.depPath = data.depPath;
      this.addDepModel.isParent = data.isParent;
      this.addDepModel.parentId = data.id;
      this.dialogVisible = true;
    },
    addDep() {
      if (!this.addDepModel) {
        this.$message.error("部门名称不能为空!");
        return false;
      }
      console.log(this.addDepModel);
      this.postRequset("/system/basic/Department/", this.addDepModel).then(
        (resp) => {
          if (resp) {
            this.addDepModel.name = "";
            this.dialogVisible = false;
            this.initDeps();
            this.defaultKeeys.push(resp.obj);
          }
        }
      );
    },
    deleteDep(data) {
      let ids = new Array();
      ids.push(data.id);
      this.$confirm(
        "此操作将永久删除【" + data.name + "】部门, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          this.deleteRequset("/system/basic/Department/", ids).then((resp) => {
            if (resp) {
              this.initDeps();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    filterNode(value, data) {
      if (!value) return true;
      return data.name.indexOf(value) !== -1;
    },
    initDeps() {
      this.getRequset("/system/basic/Department/").then((resp) => {
        this.deps = resp;
      });
    },
  },
  mounted() {
    this.initDeps();
  },
};
</script>
<style scoped>
.depBtn {
  padding: 2px;
}
</style>